CSS Selectors
Comprehensive Explanation
CSS selectors are patterns used to select and style HTML elements. They are a fundamental part of CSS and allow you to apply styles to specific elements or groups of elements on a web page.
Types of CSS Selectors:
- Element Selector: Selects all elements of a specified type.
- Class Selector: Selects elements with a specific class attribute.
- ID Selector: Selects a single element with a specific id attribute.
- Universal Selector: Selects all elements on the page.
- Attribute Selector: Selects elements based on their attributes or attribute values.
- Descendant Selector: Selects elements that are descendants of another element.
- Child Selector: Selects elements that are direct children of another element.
- Adjacent Sibling Selector: Selects an element that comes immediately after another element.
- General Sibling Selector: Selects elements that are siblings of another element.
- Pseudo-class Selector: Selects elements based on a certain state or condition.
- Pseudo-element Selector: Selects and styles a part of an element.
Line-by-Line Code Examples
/* Element Selector */
p {
color: blue;
}
/* Class Selector */
.highlight {
background-color: yellow;
}
/* ID Selector */
#header {
font-size: 24px;
}
/* Universal Selector */
* {
margin: 0;
padding: 0;
}
/* Attribute Selector */
input[type="text"] {
border: 1px solid #ccc;
}
/* Descendant Selector */
article p {
line-height: 1.6;
}
/* Child Selector */
ul > li {
list-style-type: square;
}
/* Adjacent Sibling Selector */
h1 + p {
font-weight: bold;
}
/* General Sibling Selector */
h1 ~ p {
color: gray;
}
/* Pseudo-class Selector */
a:hover {
text-decoration: underline;
}
/* Pseudo-element Selector */
p::first-line {
font-variant: small-caps;
}
Line-by-Line Explanation
p { color: blue; }
: Selects all <p> elements and sets their text color to blue..highlight { background-color: yellow; }
: Selects elements with the class "highlight" and sets their background color to yellow.#header { font-size: 24px; }
: Selects the element with the ID "header" and sets its font size to 24 pixels.* { margin: 0; padding: 0; }
: Selects all elements and removes their default margin and padding.input[type="text"] { border: 1px solid #ccc; }
: Selects input elements with the type attribute set to "text" and adds a light gray border.article p { line-height: 1.6; }
: Selects <p> elements that are descendants of <article> elements and sets their line height.ul > li { list-style-type: square; }
: Selects <li> elements that are direct children of <ul> elements and changes their bullet style to squares.h1 + p { font-weight: bold; }
: Selects the first <p> element that comes immediately after an <h1> element and makes it bold.h1 ~ p { color: gray; }
: Selects all <p> elements that are siblings of <h1> elements and sets their text color to gray.a:hover { text-decoration: underline; }
: Adds an underline to links when hovered over.p::first-line { font-variant: small-caps; }
: Selects the first line of all <p> elements and displays it in small caps.
Selector Specificity and Cascading
When multiple selectors target the same element, CSS uses specificity rules to determine which styles to apply:
- Inline styles (highest specificity)
- ID selectors
- Class selectors, attribute selectors, and pseudo-classes
- Element selectors and pseudo-elements (lowest specificity)
The cascade determines which styles take precedence when rules conflict. It considers:
- Importance (!important declaration)
- Specificity
- Source order (later rules override earlier ones)
Understanding selector specificity and the cascade is crucial for writing efficient and maintainable CSS code.
Best Practices for Using CSS Selectors
- Use meaningful class names that describe the element's purpose or content.
- Avoid overly specific selectors to improve code maintainability.
- Utilize the cascade to your advantage by structuring your CSS from general to specific.
- Group related selectors to reduce repetition and improve readability.
- Use comments to explain complex selectors or their purpose in your stylesheets.
- Consider using a naming convention like BEM (Block Element Modifier) for larger projects.
- Be cautious with universal selectors (*) as they can impact performance on large sites.
- Use ID selectors sparingly, as they have high specificity and can make overriding styles difficult.